package com.cloudpark.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.cloudpark.common.util.JsonBinder;
import com.cloudpark.common.util.MessageUtil;
import com.cloudpark.common.util.ObjectUtil;
import com.cloudpark.common.util.Pagination;
import com.cloudpark.model.entity.AdminUser;
import com.cloudpark.service.ParkRecordService;

@Controller
@RequestMapping("/record")
public class RecordController extends BaseController {

    private static final Logger log = Logger.getLogger(RecordController.class);
    
    @Autowired
    private ParkRecordService parkRecordService;

    @RequestMapping(value = "/park/enterOrLeave", method = RequestMethod.GET)
    public ModelAndView enterOrLeave(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mv = new ModelAndView("record/park/enterOrLeave.jsp");
        initMenu(request, response);
        return mv;
    }
    
    @RequestMapping(value = "/queryPardRecordList", method = RequestMethod.POST)
    @ResponseBody
    public String queryPardRecordList(HttpServletRequest request,
            @RequestParam(value = "start", defaultValue = "0") int start,
            @RequestParam(value = "length", defaultValue = "10") int length, @RequestParam(value = "draw") int draw,
            HttpServletResponse response) {
        String result = "";
        try {
            AdminUser adminUser = isSessionExpire(request, response);
            String plateNumber = request.getParameter("plateNumber");
            String carStatus = request.getParameter("carStatus");
            String parkCode = request.getParameter("parkCode");
            String releaseType = request.getParameter("releaseType");
            Map<String, String> params = new HashMap<String, String>();
            if (ObjectUtil.isNotBlank(plateNumber)) {
                params.put("plateNumber", plateNumber);
            }
            if (ObjectUtil.isNotBlank(carStatus)) {
                params.put("carStatus", carStatus);
            }
            if (ObjectUtil.isNotBlank(parkCode)) {
            	params.put("parkCode", parkCode);
            }
            if (ObjectUtil.isNotBlank(releaseType)) {
            	params.put("releaseType", releaseType);
            }
            Pagination page = parkRecordService.queryPardRecordList(params, start, length, adminUser.getEstateId());
//            result = JsonUtil.toJSONDataTables(page, draw);
            JsonBinder jsonBinder = JsonBinder.buildBinder(true);
            result = jsonBinder.toJSONDataTables(page, draw, 
            		new String[] { "parkName", "plateNumber", "enterTime", "leaveTime", "useTime", "carType", 
            		"releaseType", "carStatus","lockStatus", "remark", "entrance", "enterOpName" });
        } catch (Exception e) {
            log.error("query enter or leave record list error :", e);
            result = MessageUtil.error("服务器忙!");
        }
        return result;
    }

    @RequestMapping(value = "/park/freeOrAbnormal", method = RequestMethod.GET)
    public ModelAndView freeOrAbnormal(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mv = new ModelAndView("record/park/freeOrAbnormal.jsp");
        initMenu(request, response);
        return mv;
    }

    @RequestMapping(value = "/park/manual", method = RequestMethod.GET)
    public ModelAndView manual(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mv = new ModelAndView("record/park/manual.jsp");
        initMenu(request, response);
        return mv;
    }

    @RequestMapping(value = "/charge/tempVehicle", method = RequestMethod.GET)
    public ModelAndView tempVehicle(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mv = new ModelAndView("record/charge/tempVehicle.jsp");
        initMenu(request, response);
        return mv;
    }

    @RequestMapping(value = "/charge/monthVehicle", method = RequestMethod.GET)
    public ModelAndView monthVehicle(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mv = new ModelAndView("record/charge/monthVehicle.jsp");
        initMenu(request, response);
        return mv;
    }

    @RequestMapping(value = "/charge/storedVehicle", method = RequestMethod.GET)
    public ModelAndView storedVehicle(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mv = new ModelAndView("record/charge/storedVehicle.jsp");
        initMenu(request, response);
        return mv;
    }

    @RequestMapping(value = "/order/mobilePay", method = RequestMethod.GET)
    public ModelAndView mobilePay(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mv = new ModelAndView("record/order/mobilePay.jsp");
        initMenu(request, response);
        return mv;
    }
    
    @RequestMapping(value = "/queryOrderRecordList", method = RequestMethod.POST)
    @ResponseBody
    public String queryOrderRecordList(HttpServletRequest request,
            @RequestParam(value = "start", defaultValue = "0") int start,
            @RequestParam(value = "length", defaultValue = "10") int length, @RequestParam(value = "draw") int draw,
            HttpServletResponse response) {
        String result = "";
        try {
            AdminUser adminUser = isSessionExpire(request, response);
            String plateNumber = request.getParameter("plateNumber");
            String parkCode = request.getParameter("parkCode");
            String payChannel = request.getParameter("payChannel");
            String carType = request.getParameter("carType");
            String carStatus = request.getParameter("carStatus");
            Map<String, String> params = new HashMap<String, String>();
            if (ObjectUtil.isNotBlank(plateNumber)) {
                params.put("plateNumber", plateNumber);
            }
            if (ObjectUtil.isNotBlank(parkCode)) {
            	params.put("parkCode", parkCode);
            }
            if (ObjectUtil.isNotBlank(payChannel)) {
            	params.put("payChannel", payChannel);
            }
            if (ObjectUtil.isNotBlank(carType)) {
            	params.put("carType", carType);
            }
            if (ObjectUtil.isNotBlank(carStatus)) {
                params.put("carStatus", carStatus);
            }
            Pagination page = parkRecordService.queryOrderRecordList(params, start, length, adminUser.getEstateId());
//            result = JsonUtil.toJSONDataTables(page, draw);
            JsonBinder jsonBinder = JsonBinder.buildBinder(true);
            result = jsonBinder.toJSONDataTables(page, draw, 
            		new String[] { "parkName", "plateNumber", "enterTime", "leaveTime", "useTime", "carType", 
            		"releaseType", "carStatus","lockStatus", "remark", "payableFee", "discountFee", "paidFee", "payType" });
        } catch (Exception e) {
            log.error("query order record list error :", e);
            result = MessageUtil.error("服务器忙!");
        }
        return result;
    }

    @RequestMapping(value = "/order/monthVehiclePay", method = RequestMethod.GET)
    public ModelAndView monthVehiclePay(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mv = new ModelAndView("record/order/monthVehiclePay.jsp");
        initMenu(request, response);
        return mv;
    }

    @RequestMapping(value = "/order/refund", method = RequestMethod.GET)
    public ModelAndView refund(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mv = new ModelAndView("record/order/refund.jsp");
        initMenu(request, response);
        return mv;
    }

    @RequestMapping(value = "/onduty", method = RequestMethod.GET)
    public ModelAndView onduty(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mv = new ModelAndView("record/onduty.jsp");
        initMenu(request, response);
        return mv;
    }
}
